package com.dxy.hms.controller;

import com.alibaba.excel.EasyExcel;
import com.dxy.hms.model.entity.RoleMenu;
import com.dxy.hms.model.dto.RoleMenuDto;
import com.dxy.hms.model.excel.MenuExcel;
import com.dxy.hms.model.vo.TreeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.dxy.hms.base.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Collections;
import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dxy.hms.model.entity.Menu;
import com.dxy.hms.service.MenuService;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 菜单表 controller
 * </p>
 *
 * @author dxy
 * @since 2024-09-24
 */
@Slf4j
@Api(tags = "菜单表接口")
@RestController
@RequestMapping("/hms/menu")
public class MenuController {

    @Autowired
    private MenuService service;

    @ApiOperation("增加")
    @PostMapping("/add")
    public Resp add(@RequestBody Menu entity) {
        service.add(entity);
        return Resp.bizSuccess();
    }

    @ApiOperation("根据id修改")
    @PostMapping("/updById")
    public Resp updById(@RequestBody Menu entity) {
        service.updById(entity);
        return Resp.bizSuccess();
    }

    @ApiOperation("根据id删除")
    @PostMapping("/delByIds")
    public Resp delByIds(@RequestBody List<String> ids) {
        service.delByIds(ids);
        return Resp.bizSuccess();
    }

    @ApiOperation("详情")
    @GetMapping("/getDetail")
    public Resp<Menu> getDetail(String id) {
        return Resp.bizSuccess(service.getDetail(id));
    }

    @ApiOperation("分页")
    @GetMapping("/getPage")
    public Resp<IPage<Menu>> getPage(Long pageNum, Long pageSize) {
        return Resp.bizSuccess(service.getPage(pageNum, pageSize));
    }

    @ApiOperation("获取登录用户菜单")
    @GetMapping("/getLoginUserMenu")
    public Resp<List<TreeVo>> getLoginUserMenu() {
        return Resp.bizSuccess(service.getLoginUserMenu());
    }

    @ApiOperation("获取列表")
    @GetMapping("/getList")
    public Resp<List<Menu>> getList(String parentId, String name) {
        return Resp.bizSuccess(service.getList(parentId, name));
    }

    @ApiOperation("获取树")
    @GetMapping("/getTree")
    public Resp<List<TreeVo>> getTree(String name) {
        return Resp.bizSuccess(service.getTree(name));
    }

    @ApiOperation("添加角色菜单绑定")
    @PostMapping("/addRoleMenu")
    public Resp addRoleMenu(@RequestBody RoleMenuDto dto) {
        service.addRoleMenu(dto);
        return Resp.bizSuccess();
    }

    @ApiOperation("获取角色对菜单的绑定关系")
    @GetMapping("/getRoleMenu")
    public Resp<List<RoleMenu>> getRoleMenu(Long roleId) {
        if (roleId == null) {
            return Resp.bizSuccess(Collections.emptyList());
        }
        return Resp.bizSuccess(service.getRoleMenu(roleId));
    }

    @ApiOperation("获取菜单导入模板")
    @GetMapping("/getMenuTemplate")
    public ResponseEntity<Resource> getMenuTemplate() {
        try {
            // 文件名
            String fileName = "import_menu_template.xlsx";
            // 加载 Excel 模板文件
            Resource resource = new ClassPathResource("templates/" + fileName);
            // 设置响应头
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Disposition", "attachment; filename=" + fileName);

            return new ResponseEntity<>(resource, headers, HttpStatus.OK);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    @ApiOperation("导入菜单")
    @PostMapping("/importMenu")
    public Resp importMenu(@RequestParam("file") MultipartFile file) throws Exception {
        List<MenuExcel> list = EasyExcel.read(file.getInputStream())
                .head(MenuExcel.class)
                .sheet()
                .doReadSync();
        service.saveImportMenu(list);
        return Resp.bizSuccess();
    }
}